package online.sixfish.question.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import online.sixfish.common.eunmration.QueProgramResultTypeEnum;
import online.sixfish.common.eunmration.UserQueProgramRelationshipEnum;
import online.sixfish.common.utils.SecurityUtil;
import online.sixfish.question.mapper.QueProgramRecordMapper;
import online.sixfish.question.model.entity.QueProgramRecord;
import online.sixfish.question.service.UserQuestionRelationShipService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class UserQuestionRelationShipServiceImpl implements UserQuestionRelationShipService {

    @Resource
    QueProgramRecordMapper queProgramRecordMapper;

    @Override
    public Map<Integer, List<Integer>> getUserQueProgramRelation() {
        LambdaQueryWrapper<QueProgramRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QueProgramRecord::getUserId, SecurityUtil.currentUserId());
        List<QueProgramRecord> recordList = queProgramRecordMapper.selectList(wrapper);
        Map<Integer, List<Integer>> relationMap = Maps.newHashMapWithExpectedSize(recordList.size());
        List<Integer> attempList = new ArrayList<>();
        List<Integer> passList = new ArrayList<>();
        for (QueProgramRecord queProgramRecord : recordList) {
            if (Objects.equals(queProgramRecord.getResultType(),
                    QueProgramResultTypeEnum.COMPILE_FAIL.getCode())) {
                attempList.add(queProgramRecord.getQueProgramId());
            } else if (Objects.equals(queProgramRecord.getResultType(),
                    QueProgramResultTypeEnum.NOT_PASS.getCode())) {
                attempList.add(queProgramRecord.getQueProgramId());
            }else {
                passList.add(queProgramRecord.getQueProgramId());
            }
        }
            relationMap.put(UserQueProgramRelationshipEnum.TRIED.getId(),attempList);
            relationMap.put(UserQueProgramRelationshipEnum.SOLVED.getId(),passList);
            return relationMap;
        }
    }
